﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jumpton.SFA.Server.BusinessRule.CallRoute;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;

public partial class User_Route_List : PageBase
{
    private const string RESPONSE_NEW_PAGE = "RouteEdit.html?USERID={0}&BACKURL={1}";
    private const string REQUST_USER_ID = "userID";
    private decimal userID = 0;
    private int id = 0;
    private const string ROUTE_ID = "id";
    private OrgainzationManager orgManager = new OrgainzationManager();


    protected void Page_Load(object sender, EventArgs e)
    {
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Expires = 0;
        HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.AddHeader("pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("cache-control", "private");
        HttpContext.Current.Response.CacheControl = "no-cache";


        if (Request.QueryString[REQUST_USER_ID] != null)
        {
            decimal.TryParse(Request.QueryString[REQUST_USER_ID], out userID);
        }
        if (Request.QueryString[ROUTE_ID] != null)
        {
            int.TryParse(Request.QueryString[ROUTE_ID], out id);
        }
        if (txtHidOrg.Value == "")
        {
            txtHidOrg.Value = OrgHidValue;
            txtOrg.Text = OrgShowValue;
            OrgHelper.BindUsersDDLEX(MyStateManager.DomainID, txtHidOrg.Value, ddlMobileUser);
        }
        if (!IsPostBack)
        {
            DataBind();
            InitForm();
            
        }
    }

    private void InitForm()
    {
        try
        {
            if (userID > 0)
            {
                //如果用户确定删除了
                if (!string.IsNullOrEmpty(Request.QueryString["IsDel"]))
                {
                    if (id > 0)
                    {
                        RouteManager rM = new RouteManager();
                        ROUTES route = rM.Load(id);
                        if (route != null)
                        {
                            decimal userId = route.USERID;
                            decimal routeNumber = route.ROUTENUMBER;
                            route.Delete();
                        }
                    }
                }
               
            }
            ddlMobileUser.SelectedValue = userID.ToString();
            BindGrid();
        }
        catch { }
    }

    private void BindGrid()
    {
        RouteManager rM = new RouteManager();
        decimal.TryParse(ddlMobileUser.SelectedValue, out userID);
        gvChannelType.DataSource = rM.GetUserRoute(userID);
        gvChannelType.DataBind();
     
    }

    protected void gvChannelType_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Grid_RowDataBound(sender, e);
            ROUTES route = (ROUTES)e.Row.DataItem;
            if (route != null)
            {
                                         
                    HtmlAnchor aChange = e.Row.FindControl("aChangeRoute") as HtmlAnchor;
                    HtmlAnchor lnkDel = e.Row.FindControl("aDel") as HtmlAnchor;
                    GridViewRow row = e.Row;
                    string strSort = string.Empty;
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    sb.Append(string.Format(" AND C.ORG_ID IN (SELECT ID FROM [FC_GetOrgWithParentID]('{0}','{1}'))", MyStateManager.DomainID, MyStateManager.OrgID));

                    //Find Child GridView control
                    GridView gv = new GridView();
                    gv = (GridView)row.FindControl("gvCustomerList");

                    DataView dv = new DataView();
                    DataSet ds = CustomerRouteManager.SearchCustomerForRoute(decimal.Parse(ddlMobileUser.SelectedValue), route.ROUTENUMBER, sb.ToString());
                    dv = ds.Tables[0].DefaultView;
                    if (dv.Count > 0 && gvChannelType.DataKeys[e.Row.RowIndex].Value.ToString()!="0")
                    {
                        aChange.HRef = "#";
                        aChange.Attributes["onclick"] = string.Format("openWindow('../GIS/ChangeUserRoute.aspx?UserID={0}&RouteNumber={1}', 'ChangeRoute',',,scrollbars=yes,location=no,width=910,height=640,top=0,left=0')",
                                                                ddlMobileUser.SelectedValue,
                                                                gvChannelType.DataKeys[e.Row.RowIndex].Value);
                    }
                    else
                        aChange.Visible = false;
                    lnkDel.HRef = string.Format("User_Route_List.aspx?id={0}" + "&IsDel=Y" + "&userID={1} ", route.ID, userID.ToString());
                    gv.DataSource = dv;
                    gv.DataBind();
                    
                
            }
        }

    }
    protected void ddlMobileUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid();

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect(String.Format(RESPONSE_NEW_PAGE, ddlMobileUser.SelectedValue,Request.Url));
    }


    protected void btnExport_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("..\\ExportExcel\\ExportExcel.aspx?Table=UserRouteList&User=" + this.ddlMobileUser.SelectedValue + "&VALID=1");
    }

  
    protected void txtOrg_TextChanged(object sender, EventArgs e)
    {
        this.gvChannelType.DataSource = null;
        this.gvChannelType.DataBind();
        OrgHelper.BindUsersDDLEX(MyStateManager.DomainID, txtHidOrg.Value, ddlMobileUser);
        //ddMobileUser
        this.InitForm();
    }


    //初始化路线
    protected void btnAdd0_Click(object sender, EventArgs e)
    {
        string userid = ddlMobileUser.SelectedValue;
        if (userid != null && userid != "")
        {
            int result=RouteManager.InitRoute(userid, MyStateManager.Name);
            if ( result==0)
            {
                ShowMessage("操作成功");
                ddlMobileUser_SelectedIndexChanged(sender, e);
            }
            else
            {
                ShowMessage("操作失败");
            }
        }
    }
    protected void btnUpadteRoute_Click(object sender, EventArgs e)
    {
        Response.Redirect("User_Customer_Maintain.aspx?MobileUserID="+ddlMobileUser.SelectedValue);
    }
}
